package com.itzb.asone.admin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itzb.asone.admin.constant.AdminConstant;
import com.itzb.asone.application.BaseController;
import com.itzb.asone.constant.SystemParamConstant;
import com.itzb.asone.constant.model.SysUserConstant;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysGroup;
import com.itzb.asone.system.model.SysGroupUser;
import com.itzb.asone.system.model.SysOrg;
import com.itzb.asone.system.model.SysUser;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysDictService;
import com.itzb.asone.system.service.ISysGroupService;
import com.itzb.asone.system.service.ISysGroupUserService;
import com.itzb.asone.system.service.ISysOrgService;
import com.itzb.asone.system.service.ISysParamService;
import com.itzb.asone.system.service.ISysUserService;
import com.itzb.asone.utils.CipherUtil;
import com.itzb.asone.utils.GitId;
import com.itzb.asone.utils.JsonResult;
import com.itzb.asone.utils.PageResult;

@Controller
@RequestMapping(value = "sysuser")
public class UserController extends BaseController {
	
	private Logger logger = Logger.getLogger(getClass()); 
	
	@Autowired
	private IMessageService messageService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysDictService sysDictService;
	@Autowired
	private ISysParamService sysParamService;
	@Autowired
	private ISysGroupService sysGroupService;
	@Autowired
	private ISysGroupUserService sysGroupUserService;
	@Autowired
	private ISysOrgService sysOrgService;
	
	

	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public String list(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		return "system/sysuser/list";
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getdata", method = { RequestMethod.POST })
	public PageResult getData(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int page,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int pageSize,
			@RequestParam(required = false, value = "order") String order,
			@RequestParam(required = false, value = "sort") String sort,
			HttpServletRequest request,HttpServletResponse response
			) {
		PageResult pageResult = new PageResult();	
		Criteria<SysUser> param = new Criteria<SysUser>();
		param.setPageSize(pageSize).setStartIndex(page);
		param.setOrderBy(sort + " " + order);
		List<SysUser> list = sysUserService.queryPage(param);
		List<Map<String, Object>> listMap = sysDictService.translateToMapList(list);
		pageResult.setTotal(sysUserService.queryPageCount(param));
		pageResult.setRows(listMap);
		return pageResult;
	}
	
	/**
	 * 显示新增用户页面
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping(value = "/showmodify", method = { RequestMethod.GET })
	public String showModify(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("id");
		SysUser sysUser = new SysUser();
		if(id != null && !id.equals("")){
			sysUser = sysUserService.queryEntityById(id);
		}
		modelMap.put("sysUser", sysDictService.translateToMap(sysUser));
		return "system/sysuser/modify";
	}

	/**
	 * 新增用户
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public JsonResult modify(SysUser sysUser,HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		SysGroupUser sysGroupUser = new SysGroupUser();
		String showtype = request.getParameter("showtype");
		Criteria<SysUser> paramSysUser = new Criteria<SysUser>();
		if(showtype.equals(AdminConstant.SHOW_TYPE_UPDATE)){
			paramSysUser.addParam("id", sysUser.getId());
			paramSysUser.setRecord(sysUser);
			sysUserService.updateByCriteria(paramSysUser);

			Criteria<SysGroupUser> paramSysGroupUser = new Criteria<SysGroupUser>();
			paramSysGroupUser.addParam("userId", sysUser.getId());
			sysGroupUser = sysGroupUserService.queryPage(paramSysGroupUser).get(0);
			sysGroupUser.setGroupId(sysUser.getSysGroup().getId());
			paramSysGroupUser.setRecord(sysGroupUser);
			sysGroupUserService.updateByCriteria(paramSysGroupUser);
			
		} else {
			sysUser.setId(GitId.primaryKey());
			String password = CipherUtil.generatePassword(sysUser.getUserName(), sysParamService.getParamValue(SystemParamConstant.USER_DEFAULT_PASSWORD));
			sysUser.setPassword(password);
			sysUser.setCreateTime(new Date());
			sysUserService.insert(sysUser);
			
			sysGroupUser.setId(GitId.primaryKey());
			sysGroupUser.setGroupId(sysUser.getSysGroup().getId());
			sysGroupUser.setUserId(sysUser.getId());
			sysGroupUserService.insert(sysGroupUser);
			
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}
	
	
	/**
	 * 删除用户
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/del", method = { RequestMethod.POST })
	public JsonResult del(HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		Criteria<SysUser> param = new Criteria<SysUser>();
		SysUser sysUser = new SysUser();
		String[] ids = request.getParameterValues("id[]");
		for(int i=0;i<ids.length;i++){
			param.clearParams();
			param.addParam("id", ids[i]);
			sysUser.setStatus(SysUserConstant.T_SYS_USER_STATUS_2);
			param.setRecord(sysUser);
			sysUserService.updateByCriteria(param);
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}
	
	/**
	 * 显示新增用户页面
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping(value = "/info", method = { RequestMethod.GET })
	public String info(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(id != null && !id.equals("")){
			SysUser sysUser = sysUserService.queryEntityById(id);
			map = sysDictService.translateToMap(sysUser);
			SysGroup sysGroup = sysGroupService.getGroupByUserId(id);
			map.put("sysGroup", sysDictService.translateToMap(sysGroup));
			SysOrg sysOrg = sysOrgService.queryOrgByOrgCode(sysUser.getOrgCode());
			map.put("sysOrg", sysDictService.translateToMap(sysOrg));
		}
		modelMap.put("sysUser", map);
		return "system/sysuser/info";
	}
}
